<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use App\Models\Admin\Stats\Retention\Day\Game as StatsDayGame;
use App\Models\Admin\Stats\Retention\Day\Sign as StatsDaySign;
use App\Models\Admin\StatsTask\Retention\Day\Game as TaskDayGame;
use App\Models\Admin\StatsTask\Retention\Day\Sign as TaskDaySign;
use App\Core\Traits\Numeral;

class CreateStatsRetentionTable extends Migration
{
    use Numeral;

    /**
     * 统计任务表字段
     */
    private function makeStatsTask($tableName)
    {
        return function (Blueprint $table) use ($tableName) {
            $table->id();
            $table->unsignedInteger('timed');
            $table->string('plat_code', 64)->comment('平台code');

            $table->tinyInteger('attempts')->comment('任务尝试次数')->default(0);
            $table->tinyInteger('status')->comment('状态：1初始化，2推送到队列，3开始处理，4成功，5失败')->default(1)->index();

            $table->unsignedInteger('created');
            $table->unsignedInteger('updated');

            // 唯一索引
            $table->unique(['timed', 'plat_code'], "{$tableName}_unique");
        };
    }

    /**
     * 统计数据表字段
     */
    private function makeStats($tableName)
    {
        return function (Blueprint $table) use ($tableName) {
            $table->id();
            $table->unsignedInteger('timed');
            $table->string('plat_code', 64)->comment('平台code')->index();

            $table->integer('drr_1_num')->comment('当日人数')->nullable();
            $table->integer('drr_2_num')->comment('次日人数')->nullable();
            $table->integer('drr_3_num')->comment('3日人数')->nullable();
            $table->integer('drr_7_num')->comment('7日人数')->nullable();
            $table->integer('drr_15_num')->comment('15日人数')->nullable();
            $table->integer('drr_30_num')->comment('30日人数')->nullable();

            $table->unsignedInteger('created');
            $table->unsignedInteger('updated');

            // 唯一索引
            $table->unique(['timed', 'plat_code'], "{$tableName}_unique");
        };
    }

    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        /**
         * 统计任务表【天】【游戏】
         */
        $tableName = TaskDayGame::tableName();
        // 创建表结构
        TaskDayGame::schemaConnection()->create($tableName, $this->makeStatsTask($tableName));
        // 表名注释
        TaskDayGame::dbConnection()->statement("ALTER TABLE `{$tableName}` COMMENT '统计任务--留存--天--游戏'");
        /**
         * 统计数据表【天】【游戏】
         */
        $tableName = StatsDayGame::tableName();
        // 创建表结构
        StatsDayGame::schemaConnection()->create($tableName, $this->makeStats($tableName));
        // 表名注释
        StatsDayGame::dbConnection()->statement("ALTER TABLE `{$tableName}` COMMENT '统计--留存--天--游戏'");

        /**
         * 统计任务表【天】【注册】
         */
        $tableName = TaskDaySign::tableName();
        // 创建表结构
        TaskDaySign::schemaConnection()->create($tableName, $this->makeStatsTask($tableName));
        // 表名注释
        TaskDaySign::dbConnection()->statement("ALTER TABLE `{$tableName}` COMMENT '统计任务--留存--天--注册'");
        /**
         * 统计数据表【天】【注册】
         */
        $tableName = StatsDaySign::tableName();
        // 创建表结构
        StatsDaySign::schemaConnection()->create($tableName, $this->makeStats($tableName));
        // 表名注释
        StatsDaySign::dbConnection()->statement("ALTER TABLE `{$tableName}` COMMENT '统计--留存--天--注册'");
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        // 统计任务表【天】【游戏】
        TaskDayGame::schemaConnection()->dropIfExists(TaskDayGame::tableName());
        // 统计数据表【天】【游戏】
        StatsDayGame::schemaConnection()->dropIfExists(StatsDayGame::tableName());

        // 统计任务表【天】【注册】
        TaskDaySign::schemaConnection()->dropIfExists(TaskDaySign::tableName());
        // 统计数据表【天】【注册】
        StatsDaySign::schemaConnection()->dropIfExists(StatsDaySign::tableName());
    }
}
